<?php
tsLoad('/core/CoreLib/AbstractMysqlPage.class.php');
class FinancemanagePage extends AbstractMysqlPage {
	/**
	 * 取得总的行数
     */
    protected function calTotalRows ()
    {
        global $mysqlTools;
       $condition = $mysqlTools->check_input($this->condition);
        
       	$sqlCore = "
        	SELECT 
			    COUNT(*) AS totalCount
			FROM
			    `withd_info` `w`
        ";
        
        $where = "
         	WHERE 1=1
        ";
        //提现开始时间与结束时间
        if(!empty($condition['withdCreatetimeStart'])){
        	$where.= ' AND w.withdCreatetime >='.$condition['withdCreatetimeStart'];
        }
        if(!empty($condition['withdCreatetimeEnd'])){
        	$where.= ' AND w.withdCreatetime <='.$condition['withdCreatetimeEnd'];
        }
        //状态
        if(!empty($condition['withdState']) && $condition['withdState']!=4){
        	$where.= ' AND w.withdState ='.$condition['withdState'];
        }
        $sql = $sqlCore.$where;
        //总记录数
        $totalRows = $mysqlTools->selectSql($sql)[0]['totalCount'];
        
        return $totalRows;
    }

	/** 
     *取得数据集合
     */
    protected function getList ($sRowNo, $pageRows)
    {
        global $mysqlTools;
        //保存一个原生值以便需要的时候进行处理
        $condition = $mysqlTools->check_input($this->condition);
        
        $sqlCore = "
        	SELECT 
		    w.withdId,
		    w.withdMo,
		    w.withdCounterMo,
		    w.userId,
		    w.userLoginId,
		    w.withdTotalMo,
		    w.withdCreatetime,
		    w.withdPassTime,
		    w.withdState,
		    w.withdPassUserId,
		    w.withdPassLoginId,
        	w.withBankNo,
        	w.withBankName,
        	w.withBankAccount,
        	w.withBankUserName
		FROM
		    withd_info w
        ";
        
        $where = "
         	WHERE 1=1
        ";
     //提现开始时间与结束时间
        if(!empty($condition['withdCreatetimeStart'])){
        	$where.= ' AND w.withdCreatetime >='.$condition['withdCreatetimeStart'];
        }
        if(!empty($condition['withdCreatetimeEnd'])){
        	$where.= ' AND w.withdCreatetime <='.$condition['withdCreatetimeEnd'];
        }
        //状态
        if(!empty($condition['withdState']) && $condition['withdState']!=4){
        	$where.= ' AND w.withdState ='.$condition['withdState'];
        }
        if(!empty($condition['withdId'])){
        	$where.= ' AND  w.withdId ='.$condition['withdId'];
        }
        $where.= ' order by  w.withdCreatetime desc,w.withdPassTime desc';
        $limit = " LIMIT $sRowNo,$pageRows";
        $sql = $sqlCore.$where.$limit;
        
        //查询角色信息分页
        $list = $mysqlTools->selectSql($sql);
        
        return $list;
    }
    
    /**
     * 提现审核
     */
    public function upWithdSta($condition){
    	global $mysqlTools;
    	$mysqlTools->startTransaction();
    	$result=$this->updateWithdSta($condition);
    	if($condition['withdState']==3){
    		$mon=$this->getWithdMon($condition['withdId']);
    	
    		$mone=$mon['withdMo']+$mon['withdCounterMo'];//提现金额
    		
    		$result=$this->updateAddGlods($mon['userId'],$mone);//退回提现金额
    		
    		$a=$this->getUserGold($mon['userId']);
    		$userInFSurGlod=$a['userGold'];//剩余野猪币
    		
    		//提现角色
    		$roles=$this->getRoles($mon['userId']);
    		$roleId=$roles['roleId'];
    		$roleDesc=$roles['roleDesc'];
    		
    		//加用户收支明细表
    		$result=$this->addLogGold(1,$mon['userId'],$mon['userLoginId'],$mone,10,$userInFSurGlod,$roleId,$roleDesc);
    	}
    	if($condition['withdState']==2){
    		$mon=$this->getWithdMon($condition['withdId']);
    		$money=$mon['withdCounterMo'];
    		//添加系统收益明细
    		$sysProInId=getCurrentId();
    		$mysqlTools->executeSql("
    				insert into sys_profit_info(
    				sysProInId,
    				sysProInCreatetime,
    				sysProInReason,
    				sysProInMo
    		)values(
    				$sysProInId,
    				NOW(),
    				4,
    				$money
    		)");
    	}
    	$mysqlTools->commitTransaction();
    	return $result;
    }
    
    /**
     * 修改状态
     */
    public function updateWithdSta($condition){
    	global $mysqlTools;
    	//参数处理注入SQL安全问题
    	$condition = $mysqlTools->check_input($condition);
    	try {
    		$sql = "UPDATE `withd_info` SET `withdState` = $condition[withdState]";
    		$sql.=" , `withdPassUserId` = $condition[userId]";
    		$sql.=" , `withdPassLoginId` = $condition[userLoginId]";
    		$sql.=" , `withdPassTime` = now()";
    		$sql.=" where `withdId` = $condition[withdId]";
    		$mysqlTools->startTransaction();
    		$result = $mysqlTools->executeSql($sql);
    		$mysqlTools->commitTransaction();
    	} catch (\Exception $e) {
    		$mysqlTools->rollback();
    	}
    	return $result;
    }
    /**
     * 退回野猪币
     */
    public function updateAddGlods($userId,$userGold){
    	global $mysqlTools;
    
    		$sql = "UPDATE `sys_user` SET userGold =userGold+".$userGold." where userId = '".$userId."'";
    	
    		$result = $mysqlTools->executeSql($sql);
    		
    	return $result;
    }
    /**
     * 得到此次提现金额
     */
public function getWithdMon($condition) {
		global $mysqlTools;
		
		 $withdMo= $mysqlTools->selectSql("
	            SELECT
	            withdMo,
		 		userId,
		 		userLoginId,
		 		withdCounterMo
	            FROM
	            withd_info where
	            withdId = $condition
	            ");
			return ($withdMo[0]);
	}
	 /**
     * 用户收支明细表日志  1得到 2失去
     */
    public function addLogGold($userInFZf,$userId,$userLoginId,$userInFMo,$userInFReason,$userInFSurGlod,$roleId,$roleDesc){
    global $mysqlTools;
    $unis=getCurrentId();
    	$sql="insert into user_bop_info(userInFId,userInFZf,userInFUserId,userInFLoginId,userInFCreatetime,userInFType,userInFMo,userInFReason,userInFSurGlod,roleId,roleDesc) ";
    	$sql.=" values($unis,$userInFZf,$userId,'$userLoginId',now(),6,$userInFMo,$userInFReason,$userInFSurGlod,$roleId,'$roleDesc');";
    	$mysqlTools->startTransaction();
    	$result = $mysqlTools->executeSql($sql);
    	$mysqlTools->commitTransaction();
    	return $result;
    }
	/**
	 * 得到我的野猪币
	 */
	public function getUserGold($condition) {
		global $mysqlTools;
	
		$userGold= $mysqlTools->selectSql("
				SELECT
				su.userGold
				FROM
				sys_user su where
				su.userId = $condition
				");
		return ($userGold[0]);
	}
	/**
	 * 通过usernId得到角色
	 */
	public function getRoles($condition) {
		global $mysqlTools;
		$condition = $mysqlTools->check_input($condition);
		$rolerId= $mysqlTools->selectSql("
				SELECT
				sr.roleId,
				sr.roleDesc
				FROM
				sys_role sr
				left join sys_user_role sur on sur.roleId=sr.roleId
				where sur.userId = $condition
				");
		return ($rolerId[0]);
	}
}

?>